<?php

class BranchHelper extends Helper {
    
    private $check_rule;
    private $_field;
    
    public function __construct() {
        $this->check_rule = array(
            'br_code' => array('require', '网点编号不能为空'),
            'br_name' => array('require', '网点名称不能为空'),
            'br_rgid' => array('require', '所属大区不能为空'),
            //'br_uid' => array('require', '所属拓展经理不能为空'),
            'br_province' => array('require', '省份不能为空'),
            'br_city' => array('require', '城市不能为空'),
            'br_model' => array('require', '网点业务模式不能为空')
        );
        $this->contract_check_rule = array(
            'bc_cid' => array('require', '合同编号不能为空'),
            'bc_cname' => array('require', '合同名不能为空'),
            'bc_date' => array('require', '合同签署日期不能为空'),
            'bc_details' => array('require', '合同条款不能为空')
            );
        $this->_field = array('br_id', 'br_code', 'br_name', 'br_rgid', 'br_uid', 'br_province', 'br_city', 'br_model', 'br_bank', 'br_payee', 'br_acc_bank', 'br_acc_no', 'br_max_rate','br_bank_name','br_bank_city','br_audit_details','br_audit_value');
        $this->_u_bank_arr = array('u_id','u_realname','u_bank');
    }
    /**
     * 单条数据
     * @param type $where
     * @return type
     */
    public function branchRow($where){
        if(is_numeric($where)){
            $where = array('`br_id` = ?' => $where);
        }
        $branchModel = $this->LoadModel('Public'); 
        $data = $branchModel->getOneRow('branch', $this->_field, $where);
        if(!empty($data['br_bank'])){
            $data['br_bank_arr'] = explode(',', $data['br_bank']);
        }
        return $data;
    }
    /**
     * 
     * @param type $where
     * @return type
     */
    public function branchAll($where){
        $model = $this->LoadModel('Public'); 
        $data = $model->getList('branch', $this->_field, $where);
        return $data;
    }

   /**
     * 
     * 获取网点所有人员
     */
    public function getAllMemeber($where){
        if(is_numeric($where)){
        $where = array('`u_dp_id` = ?' => $where);
    }
        $where['`u_dp_type` =?'] = 'dot';
        $model = $this->LoadModel('Public'); 
        $data = $model->getList('member', $this->_u_bank_arr, $where);
        return $data;
    }
    
    /**
     * 数据保存
     * @param type $data
     * @param type $where 
     * @return type
     */
    public function branchSave($data, $where = ''){
        if(is_numeric($where)){
            $where = array('`br_id` = ?' => $where);
        }
        $validator = Validator::checkRule($data, $this->check_rule);
        if(!$validator['pass']){
            return array('success' => false, 'msg' => $validator['msg']);
        }
        if(!isset($data['br_bank'])){
            $data['br_bank'] = array();
        }
        $branchModel = $this->LoadModel('Public'); 
        $region_row = $branchModel->getOneRow('region','*',array('`rg_id` = ?' => $data['br_rgid']));
        $group_name = $region_row['rg_name'];
        $old_wg_id = $this->getGroupId($group_name);
        $olg_wg_pym = $data['br_city'] == '宁波市'? '宁波市': $data['br_province'];
        $wg_data = array(
           'wg_bh' => $data['br_code'],
           'wg_name' => $data['br_name'],
           'wg_pym' => $olg_wg_pym,
           'wg_group_name' => $group_name,
           'wg_board_province' => $data['br_province'],
           'wg_status' => 'ok',
           'wg_org_id' => 1, 
           'wg_board_city' => $data['br_city'],
           'wg_group' => $old_wg_id
           );
        if(is_array($data['br_bank'])) $data['br_bank'] = implode (',', $data['br_bank']);
        if(empty($where)){
            $result = $branchModel->save('ysjr_workgroup',$wg_data);
            $id = $branchModel->save('branch', $data);
            if($id && $result){
                return array('success' => true, 'msg' => $id);
            }else{
                return array('success' => false, 'msg' => '保存失败');
            }
        }else{
            $branch_msg = $branchModel->getOneRow('branch','*',$where);
            $branchModel->update('ysjr_workgroup',$wg_data,array('`wg_bh` = ?' =>$branch_msg['br_code'],'`wg_name` = ?' =>$branch_msg['br_name']));
            $branchModel->update('branch', $data, $where);
            return array('success' => true);
        }
    }
    /**
     * 分页
     * @param type $where
     * @param type $page
     * @param type $limit
     * @param type $param
     * @return type
     */
    public function branchList($where, $page = 1, $limit = PAGE_SIZE, $param = array()){
        $condition = array();
        if(!empty($param['key'])){
            $condition[ 'LOCATE(?, `br_name`) > 0 OR LOCATE(?, `br_code`) > 0'] = $param['key'];
        }
        if(!empty($param['br_rgid'])){
            $condition['`br_rgid` = ?'] = $param['br_rgid'];
        }
        $where = array_merge($where, $condition);
        $branchModel = $this->LoadModel('Public'); 
        $data['count'] = $branchModel->getRowCount('branch', $where);
        if(!empty($data['count'])){
            $data['all'] = $branchModel->getList('branch', $this->_field, $where, array($page, $limit), NULL, array('br_id DESC'));
            Pagination::SetUrl($Param);
            $data['page'] = Pagination::GetHtml($limit, $page, $data['count']);
        }
        return $data;
    }

    /**
     * 合同分页
     * @param type $where
     * @param type $page
     * @param type $limit
     * @param type $param
     * @return type
     */
    public function contractList($where,$page = 1, $limit = PAGE_SIZE, $param = array()){
        $where['`bc_status` != ?'] = 'del';
        $model = $this->LoadModel('Public');
        $data['count'] = $model->getRowCount('branch_contract',$where);
        if(!empty($data['count'])){
            $data['all'] = $model->getList('branch_contract', '*', $where, array($page, $limit), NULL, array('bc_id DESC'));
            Pagination::SetUrl($Param);
            $data['page'] = Pagination::GetHtml($limit, $page, $data['count']);
        }
        return $data;
    }

    /**
     * 
     * 获取合同信息
     */
    public function getContractinfo($where){
        if(is_numeric($where)){
            $where = array('`bc_id` = ?' => $where);
        }
        $model = $this->LoadModel('Public'); 
        $data = $model->getOneRow('branch_contract','*', $where);
        if(!empty($data['bc_contract_image'])){
            $data['bc_contract_image'] = $this->etattachmentOut($data['bc_contract_image']);
        }
        return $data;
    }

    public function etattachmentOut($data,$mode='')
    {
        if($mode){
            $mode_arr=$mode;
        }else{
            $mode_arr = array('contract_image','sign_image');
        }
        $data=unserialize($data);
        $return_data = array();
        foreach ($mode_arr as $k => $v) {
            $return_data[$v] = empty($data[$v]) ? array() : $data[$v];
            $return_data[$v . '_url'] = empty($data[$v]) ? array() : StringCode::doUrlimage($return_data[$v]);
            if (!empty($return_data[$v])) {
                foreach ($return_data[$v] as $tkey => $tval) {
                    $file_name = end(explode('/', $tval));
                    $thumb_image = str_replace($file_name, 's_' . $file_name, $tval);
                    $return_data[$v . '_thurl'][$tkey] = is_file(WEB_PATH . $thumb_image) ? StringCode::doUrlimage($thumb_image) : StringCode::doUrlimage($tval);
                }
            }
        }
        return $return_data;
    }

    /**
     * 合同删除
     * @param type $c_id
     * @return type
     */

    public function contractRemove($cid){
        $model = $this->LoadModel('Public');
        $result = $model->update('branch_contract',array('bc_status' => 'del'),array('`bc_id` = ?' =>$cid));
        return $result;
    }
    /**
     * 删除
     * @param type $where
     * @return type
     */
    public function branchRemove($where){
        if(is_numeric($where)){

            $where = array('`br_id` = ?' => $where);
        }
        $branchModel = $this->LoadModel('Public'); 
        $branch_msg = $branchModel->getOneRow('branch','*',$where);
        $branchModel->update('ysjr_workgroup',array('wg_status' => 'del'),array('`wg_bh` = ?' =>$branch_msg['br_code'],'`wg_name` = ?' =>$branch_msg['br_name']));
        $do = $branchModel->update('branch', array('br_status' => 'del'), $where);
        return $do;
    }

    /**
     * 保存审核配置
     * @param type $id 网点ID
     * @param type $data
     * @return type
     */
    public function saveAuditConfig($id,$data = array()){
        $model = $this->LoadModel('Public');
        $auditor_msg = $model->getOneRow('member','*',array('`u_realname` = ?' =>$data['auditor'],'`u_dp_id` = ?' => 4)); //4是终审部ID
        $auditor_id = $auditor_msg['u_id'];
        $msg = array(
            'br_audit_uid' => $auditor_id,
            'br_audit_urealname' => $data['auditor'],
            'br_audit_details' => $data['remark']
            );
        try{
            $model->update('branch',$msg, array('`br_id` = ?' =>$id));
            return array('success' => true, 'msg' => '保存成功');
        }catch (Exception $ex) {
            $model->rollBack();
            return array('success' => false, 'msg' => '保存异常' . $ex->getMessage());
            }
    }


    /**
    *合同保存
    *@param type $data
    *@return type
    *
    */
    public function contractSave($data){
        if($data['operate'] != 'photo'){
            if (!empty($data['branch_name'])) {
                $validator = Validator::checkRule($data, $this->contract_check_rule);
                if (!$validator['pass']) {
                    return array('success' => false, 'msg' => $validator['msg']);
                }
            }

        }
        $model = $this->LoadModel('Public');
        if(empty($data['id'])){
            $br_msg = $model->getOneRow('branch','*',array('`br_name` = ?' =>$data['branch_name']));
            $data['id'] = $br_msg['br_id'];
        }
        $msg = array(
            'bc_brid' => $data['id'],
            'bc_cid' => $data['bc_cid'],
            'bc_cname' => $data['bc_cname'],
            'bc_date' => $data['bc_date'],
            'bc_details' => $data['bc_details']
        );
        if($data['operate'] == 'photo'){
            $msg['bc_contract_image']  = (empty($data['contract_image']) && empty($data['sign_image']))?'':serialize(array('contract_image'=>$data['contract_image'],'sign_image' =>$data['sign_image']));
        }
        $check_msg = $model->getOneRow('branch_contract','*',array('`bc_id` = ?' =>$data['bcid']));
        try{
            if(empty($check_msg)){
                $model->save('branch_contract',$msg);
                return array('success' => true, 'msg' => '保存成功');
            }else{
                $model->update('branch_contract',$msg,array('`bc_id` = ?' => $check_msg['bc_id']));
                return array('success' => true, 'msg' => '保存成功');
            }
        }catch(Exception $ex){
            $model->rollBack();
            return array('success' => false, 'msg' =>'保存异常'. $ex->getMessage());
        }
    }

    /**
    *获取部门编号前缀
    *@param type $province
    *@param type $city
    *@return type
    *
    */
    public function getPrefix($province,$city){
        if($province == '北京市'){
            return '京';
        }elseif($province == '天津市'){
            return '津';
        }elseif($province == '上海市'){
            return '沪';
        }elseif($province == '重庆市'){
            return '渝';
        }elseif($province == '内蒙古自治区'){
            return '蒙';
        }elseif($province == '新疆维吾尔自治区'){
            return '新';
        }elseif($province == '广西壮族自治区'){
            return '桂';
        }elseif($province == '宁夏回族自治区'){
            return '宁';
        }elseif($province == '西藏自治区'){
            return '藏';
        }elseif($province == '香港特别行政区'){
            return '港';
        }elseif($province == '澳门特别行政区'){
            return '澳';
        }elseif($province == '黑龙江省'){
            return '黑';
        }elseif($province == '吉林省'){
            return '吉';
        }elseif($province == '辽宁省'){
            return '辽';
        }elseif($province == '河北省'){
            return '冀';
        }elseif($province == '山西省'){
            return '晋';
        }elseif($province == '青海省'){
            return '青';
        }elseif($province == '山东省'){
            return '鲁';
        }elseif($province == '河南省'){
            return '豫';
        }elseif($province == '江苏省'){
            return '苏';
        }elseif($province == '安徽省'){
            return '皖';
        }elseif($province == '福建省'){
            return '闽';
        }elseif($province == '江西省'){
            return '赣';
        }elseif($province == '湖南省'){
            return '湘';
        }elseif($province == '湖北省'){
            return '鄂';
        }elseif($province == '广东省'){
            return '粤';
        }elseif($province == '台湾省'){
            return '台';
        }elseif($province == '海南省'){
            return '琼';
        }elseif($province == '甘肃省'){
            return '甘';
        }elseif($province == '陕西省'){
            return '秦';
        }elseif($province == '四川省'){
            return '川';
        }elseif($province == '贵州省'){
            return '黔';
        }elseif($province == '云南省'){
            return '滇';
        }elseif($province == '浙江省'){
            if($city == '杭州市'){
                return '浙A';
            }elseif($city == '宁波市'){
                return '浙B';
            }elseif($city == '温州市'){
                return '浙C';
            }elseif($city == '绍兴市'){
                return '浙D';        
            }elseif($city == '湖州市'){
                return '浙E';
            }elseif($city == '嘉兴市'){
                return '浙F';
            }elseif($city == '金华市'){
                return '浙G';
            }elseif($city == '衢州市'){
                return '浙H';
            }elseif($city == '台州市'){
                return '浙J';
            }elseif($city == '丽水市'){
                return '浙K';
            }elseif($city == '舟山市'){
                return '浙L';
            }
        }
    }

    /**
     * 生成部门编号
     * @param type $province 
     * @param type $city
     * @return type
     */
    public function getBranchNo($province,$city){
        $msg = $this->getPrefix($province,$city);
        $where = array(
            "`br_code` LIKE ? " => $msg . '%'
        );
        $model = $this->LoadModel('Public');
        $code = $model->getRow('branch','*', $where,null,array('br_id DESC'));
        $max =preg_replace('/\D/s','',$code['br_code']);
        $num = 100+$max+1;
        $no = $msg . substr($num, 1, 2);
        return $no;
    }
    /**
     * 获取大区编号
     * @param type $group_name
     * @return type
     */
    public function getGroupId($group_name){
        if($group_name == '东北大区'){
            return 3131;
        }elseif ($group_name == '华北大区') {
            return 3406;
        }elseif ($group_name == '删-公司直辖'){
            return 3408;
        }elseif($group_name == '华中大区'){
            return 3132;
        }elseif($group_name == '西南大区'){
            return 3133;
        }elseif($group_name == '西北大区'){
            return 3134;
        }elseif($group_name == '华东大区'){
            return 3129;
        }elseif($group_name == '两湖大区'){
            return 3407;
        }elseif ($group_name == '华南大区') {
            return 3209;
        }elseif ($group_name == '删-苏沪大区'){
            return 3130;
        }elseif ($group_name == '长马特别行政区'){
            return 3450;
        }elseif ($group_name == '甘凉大区') {
            return 3584;
        }

    }
    /**
     * 获取大区名
     * @param type $rgid 大区ID
     * @return type
     */
    public function getRegionname($rgid){
        $model = $this->LoadModel('Public');
        $msg = $model->getOneRow('region','*',array('`rg_id` = ?' =>$rgid));
        $result = $msg['rg_name'];
        return $result;
    }

    /**
     * 关闭网点银行
     * @param type $br_id 网点ID
     * @param type $bk_id 银行ID
     * @return type
     */
    public function closeBank($br_id,$bk_id){
        $model = $this->LoadModel('Public');
        $br_msg = $model->getOneRow('branch','*',array('`br_id` = ?' => $br_id));
        $br_bank_arr = explode(",",$br_msg['br_bank']);
        foreach ($br_bank_arr as $key=>$value){
            if ($value == $bk_id){
                unset($br_bank_arr[$key]);
            }
        }
        $br_bank = implode(",",$br_bank_arr);
        $member_msg = $this->getAllMemeber($br_id);
        for($i=0;$i<count($member_msg);$i++){
            $member_bank_arr = explode(",",$member_msg[$i]['u_bank']);
            foreach ($member_bank_arr as $k => $v) {
                if($v == $bk_id){
                    unset($member_bank_arr[$k]);
                }
            }
            $member_bank = implode(",",$member_bank_arr);
            try {
                $model->update('member',array('u_bank' => $member_bank),array('`u_id` = ?' => $member_msg[$i]['u_id']));
            }catch (Exception $ex){
                $model->rollBack();
                return array('success' => false, 'msg' => '保存异常');
            }
        }
        try{
            $model->update('branch',array('br_bank' => $br_bank),array('`br_id` = ?' => $br_id));
            return array('success' => true, 'msg' => '保存成功');
        }catch (Exception $ex){
                $model->rollBack();
                return array('success' => false, 'msg' => '保存异常');
        }
    }

     /**
     * 未开通该银行网点
     * @param type $bk_id
     * @return type
     */
     public function nobankBranch($bk_id){
        $model = $this->LoadModel('Public');
        $msg = $model->getList('branch', '*', array('`br_bank` NOT LIKE ?' => '%'.$bk_id.'%'));
        for($i=0;$i<count($msg);$i++){
            $br_name_arr[$i] = $msg[$i]['br_name'];
        }
        return $br_name_arr;
     }

     /**
     * 网点开通银行端口
     * @param type $br_name
     * @param type $bk_id
     * @return type
     */
    public function openBank($br_name,$bk_id){
      $model = $this->LoadModel('Public');
      $check_msg = $model->getOneRow('branch','*',array('`br_name` =?'=> $br_name,'`br_status` =?'=> 'ok'));
      $br_member = $this->getAllMemeber($check_msg['br_id']);
      if(empty($check_msg)){
        return array('success' => false, 'msg' => '请输入正确网点名');
      }else{
        if(strpos($check_msg['br_bank'],$bk_id)!==false){
          return array('success' => false, 'msg' => '此网点已开通该银行');
        }else{
          if(empty($check_msg['br_bank'])){
            $model->update('branch',array('br_bank'=> $bk_id),array('`br_id` =?' =>$check_msg['br_id']));
            for($a=0;$a<count($br_member);$a++){
                $model->update('member',array('u_bank'=> $bk_id),array('`u_id` =?' =>$br_member[$a]['u_id']));
            }
            return array('success' => true ,'msg' => '开通成功');
          }else{
            $check_msg['br_bank'] = $check_msg['br_bank'].','.$bk_id;
            $model->update('branch',array('br_bank'=> $check_msg['br_bank']),array('`br_id` =?' => $check_msg['br_id']));
            for($a=0;$a<count($br_member);$a++){
                $model->update('member',array('u_bank'=> $check_msg['br_bank']),array('`u_id` =?' =>$br_member[$a]['u_id']));
            }
            return array('success' => true ,'msg' => '开通成功');
          }
        }
      }
    }
    /**
     * 关闭网点所有银行
     * @param type $br_id 网点ID
     * @return type
     */
    public function closeAll($br_id){
        $model = $this->LoadModel('Public');
        $check_msg = $model->getOneRow('branch','*',array('`br_id` = ?' => $br_id));
        $br_member = $this->getAllMemeber($br_id);
        $model->update('branch',array('br_bank'=>''),array('`br_id` =?' =>$br_id));
        for($i=0;$i<count($br_member);$i++){
            $model->update('member',array('u_bank'=> ''),array('`u_id` =?' =>$br_member[$i]['u_id']));
        }
        return array('success' => true ,'msg' => '关闭成功');

    }


    /**
     * 网点银行账号修改保存
     * @param type $data
     * @return type
     */
    public function bankaccSave($data){
        $model = $this->LoadModel('Public');
        $msg = $model->getOneRow('branch','*',array('`br_id` =?' => $data['br_id']));
        if(!empty($msg)){
            try{
                $model->update('branch',$data, array('`br_id` = ?' =>$data['br_id']));
                return array('success' => true, 'msg' => '保存成功');
            }catch (Exception $ex) {
                $model->rollBack();
                return array('success' => false, 'msg' => '保存异常' . $ex->getMessage());
                }
        }
    }


    public function getAllCity($province,$br_id=0){
        $cityModel = $this->LoadModel('Public');
        $screen_arr = $br_id >0? array('`display` = ?' => 2,'`unadmittance_branch` LIKE (?) OR `unadmittance_branch`="'.$br_id.'" OR `unadmittance_branch` LIKE "'.$br_id.',%" OR `unadmittance_branch` LIKE "%,'.$br_id.'"' =>'%,'.$br_id.',%') : array('`display` = ?' => 2);
        $city = $cityModel->getList('city', '*', $screen_arr);
        $select_city = array();
        foreach ($city as $key => $val) {
            if ($val['state'] == $province) {
                $select_city[] = $val;
            }
        }
        return $select_city;
    }

    public function getAllArea($city,$br_id=0){
        $model = $this->LoadModel('Public');
        $city_row = $model->getOneRow('city', '*', array('`display` = ?' => 2, '`city` = ?' => $city));
        if (empty($city_row))
            return false;
        $code = substr($city_row['sz_code'], 0, 4);
        $screen_arr = $br_id > 0? array('`display` = ?' => 1, '`sz_code` LIKE ?' => $code . '%','`unadmittance_branch` LIKE (?) OR `unadmittance_branch`="'.$br_id.'" OR `unadmittance_branch` LIKE "'.$br_id.',%" OR `unadmittance_branch` LIKE "%,'.$br_id.'"' =>'%,'.$br_id.',%') : array('`display` = ?' => 1, '`sz_code` LIKE ?' => $code . '%');
        $area = $model->getList('city', '*', $screen_arr);
        return $area;
    }
    /**
    *获取网点非准入地区及全部地区
    *@param type $br_id
    *@return type
    */
    public function getAllPlace($br_id = 0){
        $model = $this->LoadModel('Public');
        $screen = $br_id > 0? array('`display` =?' =>3,'`unadmittance_branch` LIKE (?) OR `unadmittance_branch`="'.$br_id.'" OR `unadmittance_branch` LIKE "'.$br_id.',%" OR `unadmittance_branch` LIKE "%,'.$br_id.'"' =>'%,'.$br_id.',%') : array('`display` =?' =>3);
        $province_arr = $model->getList('city','*',$screen);
        if(!empty($province_arr)){
            foreach($province_arr as $k=>$v){
                $city_arr[$v['state']] = $this->getAllCity($v['state'],$br_id);
            }
        }
        if(!empty($city_arr)){
            foreach($city_arr as $key => $val){
                foreach($val as $x => $y){
                    $area_arr[$y['city']] = $this->getAllArea($y['city'],$br_id);
                }   
            }
        }
        $data['province'] = $province_arr;
        $data['city'] = $city_arr;
        $data['area'] = $area_arr;
        return $data;

    }

    public function takeoutId($br_id = 0){
        $data = $this->getAllPlace($br_id);
        if(!empty($data['province'])){
            foreach($data['province'] as $xk => $xv){
                $id_arr[] = $xv['id'];
            }
        }
        if(!empty($data['city'])){
            foreach($data['city'] as $k => $v){
                foreach($v as $ck => $cv){
                    $id_arr[] = $cv['id'];
                }
            }
        }
        if(!empty($data['area'])){
            foreach($data['area'] as $x => $y){
               foreach ($y as $xk => $yk) {
                   $id_arr[] = $yk['id'];
               }
            }
        }
        return $id_arr;

    }


    /**
    *网点上牌地限制保存
    *@param type $diif1 关闭的地区
    *@param type $diif2 新开的地区
    *@param type $br_id 网点id
    *@return type
    */
    public function unadmittanceEdit($diff1,$diff2,$br_id){
        $model = $this->LoadModel('Public');
        $model->beginTransaction();
        try{
            if(!empty($diff1)){
                foreach($diff1 as $k => $v){
                    $data = $model->getOneRow('city','*',array('`id` =?' => $v));
                    if($data['unadmittance_branch'] == $br_id){
                        $model->update('city',array('unadmittance_branch'=>''),array('`id` =?' =>$v));
                    }else{
                        $unadmittance_arr = explode(',',$data['unadmittance_branch']);
                        $key = array_search($br_id,$unadmittance_arr);
                        unset($unadmittance_arr[$key]);
                        $unadmittance = implode(',',$unadmittance_arr);
                        $model->update('city',array('unadmittance_branch'=>$unadmittance),array('`id` =?' =>$v));
                    }
                }
            }
            if(!empty($diff2)){
                foreach($diff2 as $x => $y){
                    $data = $model->getOneRow('city','*',array('`id` =?' => $y));
                    if(empty($data['unadmittance_branch'])){
                        $unadmittance_data = $br_id;
                    }else{
                        $branch_arr = explode(',',$data['unadmittance_branch']);
                        $branch_arr[count($branch_arr)] = $br_id;
                        $unadmittance_data = implode(',',$branch_arr);
                    }
                    $model->update('city',array('unadmittance_branch'=>$unadmittance_data),array('`id` =?' =>$y));
                }
            }
            $model->commit();
            return array('success' => true ,'msg' => '保存成功');
        } catch (Exception $ex) {
            $model->rollBack();
            return array('success' => false, 'msg' => '保存异常'.$ex);
        }
    }

    //用户可以查看的网点
    public function branchScreen($where,$member_info,$page=1,$limit=100){
        if($member_info['u_dp_type'] =='rg'){
            if($member_info['u_scope'] =='部门级'){
                $where['`br_rgid` =?'] = $member_info['u_dp_id'];
            }elseif($member_info['u_scope'] == '用户级'){
                $where['`br_uid` =?'] = $member_info['u_id']; 
            }
        }
        $branchModel = $this->LoadModel('Public'); 
        $data['count'] = $branchModel->getRowCount('branch', $where);
        if(!empty($data['count'])){
            $data['all'] = $branchModel->getList('branch', $this->_field, $where, array($page, $limit), NULL, array('br_id DESC'));
            Pagination::SetUrl($Param);
            $data['page'] = Pagination::GetHtml($limit, $page, $data['count']);
        }
        return $data;
    }

    /**
    *获取 网点风控标准 报表其他信息
    *@param type $br_name 网点名
    *@param type $br_id 网点id
    *@return type
    */
    public function getOtherMsg($br_id,$br_name){
        $model = $this->LoadModel('Public');
        $contract_msg = $model->getRow('branch_contract','*',array('`bc_brid` =?' =>$br_id,'`bc_status` =?' =>'ok'),null,array('bc_date ASC'));
        $data['br_first_contracttime'] = $contract_msg['bc_date'];
        $baoe_data = $model->getList('baoe_month','*',array('`be_branch` =?' => $br_name),array(1,3),null,array('be_month DESC'));
        $data['br_yuqi'] = isset($baoe_data[0]['be_yuqi'])? $baoe_data[0]['be_yuqi'] : '暂无';
        $data['br_front_yuqi'] = isset($baoe_data[1]['be_yuqi'])? $baoe_data[1]['be_yuqi'] :'暂无';
        $data['br_ffront_yuqi'] = isset($baoe_data[2]['be_yuqi'])? $baoe_data[2]['be_yuqi'] :'暂无';
        return $data;
    }

    //每月20号执行任务
    public function timedTask(){
        $model = $this->LoadModel('Public');
        $model->update('branch',array('br_audit_details'=>''),array('`br_status` =?' =>'ok'));//清空br_audit_details
        $branch_arr = $model->getList('branch','*',array('`br_status` =?' =>'ok'));
        if(!empty($branch_arr)){
            foreach($branch_arr as $k=>$v){
                $data = $this->getOtherMsg($v['br_id'],$v['br_name']);
                $v['br_first_contracttime'] = $data['br_first_contracttime'];
                $v['br_yuqi'] = $data['br_yuqi'];
                $v['br_front_yuqi'] = $data['br_front_yuqi'];
                $v['br_ffront_yuqi'] = $data['br_ffront_yuqi'];
                if(!empty($data['br_first_contracttime']) && is_numeric($data['br_yuqi']) && (NOW_TIME-$data['br_first_contracttime'] >7776000)){ //系统中有签约时间,且签约大于三个月，有当前逾期率
                    $br_audit_value = $this->getAuditValue($v);
                }else{
                    $br_audit_value = 8;
                }
                $model->update('branch',array('br_audit_value'=>$br_audit_value),array('`br_id` =?' => $v['br_id']));
            }
        }        
    }

    //获取当期br_audit_value
    public function getAuditValue($branch_msg){
        if($branch_msg['br_yuqi']<3){
            $audit_value = 12;
        }elseif($branch_msg['br_yuqi']<4){
            $audit_value = 10;
        }elseif($branch_msg['br_yuqi']<5){
            $audit_value = 8;
        }elseif($branch_msg['br_yuqi']<10){
            if(!is_numeric($branch_msg['br_audit_value'])) $branch_msg['br_audit_value'] = 8; //第一次跑 没有原值情况
            if(is_numeric($branch_msg['br_front_yuqi'])){//有上期逾期率
                if($branch_msg['br_yuqi']>$branch_msg['br_front_yuqi']){
                    $audit_value = $branch_msg['br_audit_value'] - 1;
                }else{
                    if($branch_msg['br_audit_value'] == 5 && $branch_msg['br_yuqi']<8){
                        $audit_value = $branch_msg['br_audit_value'] + floor($branch_msg['br_front_yuqi'] - $branch_msg['br_yuqi']);
                    }else{
                        $audit_value = $branch_msg['br_audit_value'];
                    }
                }
            }else{
                $audit_value = $branch_msg['br_audit_value'];
            }
            if($audit_value>8) $audit_value = 8;
            if($audit_value<5) $audit_value = 5;
        }else{
            if(is_numeric($branch_msg['br_front_yuqi']) && is_numeric($branch_msg['br_ffront_yuqi']) && $branch_msg['br_front_yuqi']>=10 && $branch_msg['br_ffront_yuqi']>=10){
                $audit_value = 0;
            }else{
                $audit_value = 5;
            }
        }
        return $audit_value;
    }
}
